package com.luych.toolbox.docker.service.sdk.image.model;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class DockerImagesBuildParam {

    private final InputStream inputStream;
    private final String dockerfile;
    private final String t;

    private RegistryConfig registryConfig;
    private BuildArgs buildArgs;
    private Consumer<String> lineConsume;

    public DockerImagesBuildParam(InputStream inputStream, String dockerfile, String t) {
        this.inputStream = inputStream;
        this.dockerfile = dockerfile;
        this.t = t;
    }

    public static DockerImagesBuildParam getInstance(InputStream inputStream, String dockerfile, String t) {
        return new DockerImagesBuildParam(inputStream, dockerfile, t);
    }

    public void setRegistryConfig(RegistryConfig registryConfig) {
        this.registryConfig = registryConfig;
    }

    public void setBuildArgs(BuildArgs buildArgs) {
        this.buildArgs = buildArgs;
    }

    public DockerImagesBuildParam login(RegistryConfig registryConfig) {
        this.registryConfig = registryConfig;
        return this;
    }

    public DockerImagesBuildParam login(String registry, String username, String password) {
        if (this.registryConfig == null) {
            this.registryConfig = RegistryConfig.getInstance();
        }
        this.registryConfig.login(registry, username, password);
        return this;
    }

    public DockerImagesBuildParam args(BuildArgs args) {
        this.buildArgs = args;
        return this;
    }

    public DockerImagesBuildParam arg(String name, String value) {
        if (this.buildArgs == null) {
            this.buildArgs = BuildArgs.getInstance();
        }
        this.buildArgs.arg(name, value);
        return this;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getDockerfile() {
        return dockerfile;
    }

    public String getT() {
        return t;
    }

    public RegistryConfig getRegistryConfig() {
        return registryConfig;
    }

    public BuildArgs getBuildArgs() {
        return buildArgs;
    }

    public Consumer<String> getLineConsume() {
        return lineConsume;
    }

    public DockerImagesBuildParam onLine(Consumer<String> lineConsume) {
        this.lineConsume = lineConsume;
        return this;
    }

    public static class BuildArgs extends HashMap<String, String> {

        public static BuildArgs getInstance(Map<String, String> args) {
            BuildArgs buildArgs = new BuildArgs();
            buildArgs.putAll(args);
            return buildArgs;
        }

        public static BuildArgs getInstance() {
            return new BuildArgs();
        }

        public BuildArgs arg(String name, String value) {
            this.put(name, value);
            return this;
        }
    }

    public static class RegistryConfig extends HashMap<String, RegistryConfig.UserPass> {

        public static RegistryConfig getInstance() {
            return new RegistryConfig();
        }

        public RegistryConfig login(String registry, String username, String password) {
            this.put(registry, new UserPass(username, password));
            return this;
        }

        public static class UserPass {
            private String username;
            private String password;

            public UserPass(String username, String password) {
                this.username = username;
                this.password = password;
            }

            public String getUsername() {
                return username;
            }

            public String getPassword() {
                return password;
            }
        }

    }

}
